package luke4fun.wheresmymoney.service.async;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import luke4fun.wheresmymoney.adapter.list.impl.BudgetInfoListAdapter;
import luke4fun.wheresmymoney.bean.BudgetBean;
import luke4fun.wheresmymoney.bean.BudgetDetailBean;
import luke4fun.wheresmymoney.db.AccDBHelper;
import luke4fun.wheresmymoney.db.DBConstants;

public class BudgetInfoAsyncTask extends AsyncTask<Long, Long, List<BudgetBean>> {

	private Context context;
	private BudgetInfoListAdapter adapter;
	
	public BudgetInfoAsyncTask(Context context, BudgetInfoListAdapter adapter) {
		this.context = context;
		this.adapter = adapter;
	}
	
	@Override
	protected List<BudgetBean> doInBackground(Long... params) {
		AccDBHelper dbHelper = new AccDBHelper(context);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		
		/*
		 * budget._id, budget_from, budget_to, budget
		 * budget_detail._id, budget_detail.pid, budget_detail.category, budget_detail.budget,
		 * category._id, category.name, category.color, category.frequence 
		 */
		StringBuilder sb = new StringBuilder("SELECT * FROM " + DBConstants.V_BUDGET);
		
		
		Cursor cursor = db.rawQuery(sb.toString(), null);
//		Log.d("ZJ", "columns: " + cursor.getColumnCount());
//		for (int i=0; i<cursor.getColumnCount(); i++) {
//			Log.d("ZJ", "column("+i+"): " + cursor.getColumnName(i));
//		}
		
		Map<Long, BudgetBean> beanMap = new HashMap<Long, BudgetBean>();
		while (cursor.moveToNext()) {
			
			// budget
			Long budgetId   = cursor.getLong(0);
			Long budgetFrom = cursor.getLong(1);
			Long budgetTo   = cursor.getLong(2);
			Integer budget  = cursor.getInt(3);
			// detail
			Long dtlId		= cursor.getLong(4);
			Long dtlPid		= cursor.getLong(5);
			Long dtlCategory = cursor.getLong(6);
			Integer dtlBudget= cursor.getInt(7);
			// category
//			Long categoryId 	  = cursor.getLong(8);
			String categoryName   = cursor.getString(9);
			Integer categoryColor = cursor.getInt(10);
			// frequence: 11
			// expense sum
			Long expense = cursor.getLong(12);
			
			BudgetBean bean 	 = beanMap.get(budgetId);
			if (bean == null) {
				bean = new BudgetBean();
				bean.setId(budgetId);
				bean.setFrom(budgetFrom);
				bean.setTo(budgetTo);
				bean.setBudget(budget);
			}
			
			if (dtlBudget != 0) {
				BudgetDetailBean dtl = new BudgetDetailBean();
				dtl.setId(dtlId);
				dtl.setPid(dtlPid);
				dtl.setCategoryId(dtlCategory);
				dtl.setCategory(categoryName);
				dtl.setCategoryColor(categoryColor);
				dtl.setBudget(0);
				dtl.setBudget(dtlBudget);
				dtl.setMax(budget);
				dtl.setExpense(expense);
	
				bean.getDetails().add(dtl);
				dtl.setParent(bean);
				
				beanMap.put(budgetId, bean);
			}
		}
		
		db.close();
		dbHelper.close();
		
		List<BudgetBean> list = new ArrayList<BudgetBean>();
		list.addAll(beanMap.values());
		return list;
	}

	@Override
	protected void onPostExecute(List<BudgetBean> list) {
		adapter.updateList(list);
	}
}
